package com.damage.business_dept.web;

import com.damage.business_dept.oa.makePurchasePlan.AuditService;
import com.damage.common.dto.AuditResult;
import com.damage.common.oa.makePurchasePlan.MakePurchaePlanWorkflowService;
import com.damage.common.oa.makePurchasePlan.MakePurchasePlanConfig;
import com.damage.common.object.BaseResp;
import com.damage.common.util.UserUtil;
import com.damage.common.util.Variable;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/epms/purchasePlan/audit")
@ResponseBody
@Slf4j
public class PurchasePlanAuditController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private AuditService auditService;

    /**
     * 签收审核任务
     * @param taskId
     * @param session
     * @return
     */
    @PostMapping("/claim/{id}")
    public BaseResp claimAuditTask(@PathVariable("id")String taskId, HttpSession session){
        String userId = UserUtil.getUserFromSession(session).getId();
        taskService.setVariable(taskId, MakePurchasePlanConfig.CLAIM_TASK_USER,userId);
        taskService.claim(taskId,userId);
        log.info("任务:{}，被:{}签收了",taskId,userId);
        return BaseResp.ok();
    }

    /**
     * 完成审核任务
     * @param taskId
     * @param auditResult
     * @return
     */
    @PostMapping("/complete/{id}")
    public BaseResp completeAuditTask(@PathVariable("id")String taskId, AuditResult auditResult){
        try {
            auditService.auditPurchasePlan(taskId,auditResult);
            return BaseResp.ok();
        } catch (Exception e) {
            log.error("完成任务失败，任务taskId:{}",taskId);
            e.printStackTrace();
            return BaseResp.error(e.getMessage());
        }
    }
}
